<template>
  <div class="app-container search-list">
    <table-pagination
        ref="el-search-table"
        :adaptive="fixOption"
        :columns="columns"
        :fetch="getData"
        :form-options="formOptions"
        :page-sizes="[20, 50, 100]"
        height="300"
        list-field="records"
        method="get"
        page-index-key="page"
        page-size-key="pageSize"
        total-field="totalElements"
        type="remote">
      <template #button>
        <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handleAdd">新建活动</el-button>
      </template>
      <el-table-column slot="append" label="操作" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button plain size="mini" type="primary" @click="dataDetail(scope.row.id)">数据</el-button>
          <el-button plain size="mini" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button v-if="scope.row.status=='ing'" plain size="mini" type="primary"
                     @click="endCouponCode(scope.row.id)">
            结束
          </el-button>
          <el-button v-if="scope.row.status=='not_start'" plain size="mini" type="danger"
                     @click="deleteCouponCode(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <template slot="timeRange" slot-scope="scope">
        {{
          scope.row.beginDate|utcFormat('YYYY-MM-DD HH:mm:ss', timeZone)
        }} - <span v-if="scope.row.endDate==undefined">永不过期</span>
        <span v-if="scope.row.endDate!=undefined">
          {{ scope.row.endDate|utcFormat('YYYY-MM-DD HH:mm:ss', timeZone) }}
        </span>
      </template>
      <template slot="rule" slot-scope="scope">
        满{{ scope.row.consumeMoney|currency('$') }}
        <span v-if="scope.row.marketingType=='discount'">减{{ scope.row.reduction }}%</span>
        <span v-if="scope.row.marketingType!='discount'">减{{ scope.row.reduction|currency('$') }}</span>
      </template>
      <template slot="totalCount" slot-scope="scope">
        <span v-if="scope.row.joinCount==undefined">不限</span>
        <span v-if="scope.row.joinCount!=undefined">{{ scope.row.joinCount }}</span>
      </template>
      <template slot="statusText" slot-scope="scope">
        <span v-if="scope.row.status=='not_start'" style="color: rgb(255,153,0)">{{ scope.row.statusText }}</span>
        <span v-if="scope.row.status=='ing'" style="color: rgb(0,128,0)">{{ scope.row.statusText }}</span>
        <span v-if="scope.row.status=='end'" style="color: rgb(153,153,153)">{{ scope.row.statusText }}</span>
        <span v-if="scope.row.status=='pause'" style="color: rgb(255,0,0)">{{ scope.row.statusText }}</span>
      </template>
    </table-pagination>
  </div>
</template>

<script>
import TablePagination from '_c/Tables/table-pagination';
import {getCouponCodePage, endCouponCode, deleteCouponCode} from '@/api/coupon-code';
import {mapGetters} from 'vuex';

export default {
    name: 'coupon-codes',
    components: {TablePagination},
    data () {
        return {
            marketingTypeOption: [{
                value: 'discount',
                label: '折扣券'
            }, {
                value: 'reduction',
                label: '代金券'
            }],
            fixOption: {fixedHeader: true},
            pageParams: {
                page: 1,
                pageSize: 20,
                name: '',
                couponCode: '',
                marketingType: '',
                status: ''
            },
            activityState: [
                {
                    value: '',
                    label: '全部'
                },
                {
                    value: 'not_start',
                    label: '未开始'
                }, {
                    value: 'ing',
                    label: '进行中'
                }, {
                    value: 'end',
                    label: '已结束'
                }
            ],
            formOptions: {
                showResetBtn: true,
                showSearchBtn: true,
                inline: false,
                labelWidth: 120,
                submitBtnText: '查询',
                size: 'small',
                resetBtnCallback: () => {
                    this.$refs['el-search-table'].resetFields();
                    this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                },
                forms: [
                    {prop: 'name', label: '活动名称：', placeholder: '请输入活动名称'},
                    {prop: 'couponCode', label: '优惠码：', placeholder: '请输入优惠码'},
                    {
                        prop: 'status',
                        label: '活动状态：',
                        itemType: 'select',
                        // placeholder: '请选择折扣类型',
                        selectFetch: () => {
                            return new Promise((resolve, reject) => {
                                resolve(this.activityState);
                            });
                        }
                    },
                    {
                        prop: 'marketingType',
                        label: '折扣类型：',
                        itemType: 'select',
                        // placeholder: '请选择折扣类型',
                        selectFetch: () => {
                            return new Promise((resolve, reject) => {
                                resolve(this.marketingTypeOption);
                            });
                        }
                    }
                ]
            },
            columns: [
                {label: '活动名称', prop: 'name', showOverflowTooltip: true},
                {label: '折扣类型', prop: 'marketingTypeText'},
                {label: '优惠码', prop: 'couponCode', width: 100},
                {label: '规则', slotName: 'rule', width: 200, showOverflowTooltip: true},
                {label: '有效日期', slotName: 'timeRange', width: 300, showOverflowTooltip: true},
                {label: '总数', slotName: 'totalCount', width: 100},
                {label: '已领取', prop: 'useCount', width: 100},
                {label: '状态', slotName: 'statusText', width: 100}
            ]
        };
    },
    methods: {
        getData (params) {
            params.siteId = this.siteId;
            return new Promise((resolve, reject) => {
                getCouponCodePage(params).then((res) => {
                    resolve(res);
                });
            });
        },
        handleSearch () {
            this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
        },
        // 新建活动
        handleAdd () {
            this.$router.push({name: 'coupon-codes-add'});
        },
        // 优惠码编辑
        handleEdit (id) {
            this.$router.push({name: 'coupon-codes-edit', params: {id}});
        },
        dataDetail (id) {
            this.$router.push({name: 'coupon-codes-data', params: {id}});
        },
        deleteCouponCode (id) {
            this.$confirm('此操作将删除活动, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCouponCode(id).then(res => {
                    if (res.code === '200') {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });
        },
        endCouponCode (id) {
            this.$confirm('此操作将结束活动, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                endCouponCode(id).then(res => {
                    if (res.code === '200') {
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        });
                        this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });
        }
    },
    computed: {
        ...mapGetters(['timeZone']),
        siteId () {
            return this.$store.state.user.siteId;
        }
    }
}
;
</script>

<style lang="scss" scoped>
.search-list {
  h4 {
    border-bottom: 1px solid #E8E8E8;
    margin: 0;
    margin-bottom: 10px;
    padding-bottom: 10px;
  }
}
</style>
